#include <iostream>
#include "xmltabledialog.h"

XMLTableDialog::XMLTableDialog()
{
setupUi(this);
ignored_rows = 1;
QObject::connect( spinBox,
                  SIGNAL( valueChanged(int) ),
                  this,
                  SLOT( reset_ignored_rows(int) ) );
}

XMLTableDialog::~XMLTableDialog()
{
}

void XMLTableDialog::set_model( const QList< QMap<int, QString> > & data,
                                bool ncat_export )
{
XMLTabModel * model = new XMLTabModel;
model->set_data(data, ncat_export);
int column_count = model->columnCount();
tableView->setModel(model);
    for ( int i = 0; i < column_count; i++ )
    {
    tableView->setColumnWidth( i, 140 );
    }
tableView->setItemDelegateForRow( 0, new ComBoxDelegate() );
}

void XMLTableDialog::reset_ignored_rows(int n)
{
ignored_rows = n;
XMLTabModel * model = qobject_cast<XMLTabModel *>( tableView->model() );
    if (NULL != model)
    {
    model->set_ignored_rows(n);
    }
}

int XMLTableDialog::return_ignored_rows()
{
return ignored_rows;
}

QMap<int, int> XMLTableDialog::return_columns()
{
QMap<int, int> m;
XMLTabModel * model = qobject_cast<XMLTabModel *>( tableView->model() );
    if ( NULL == model )
    {
    return m;
    }

Schema * schema = new Schema;
    if ( NULL == schema )
    {
    return m;
    }
QMap<int, QString> headers = schema->headers();

QString ignore = tr("Skip Column");
QString desc = tr("Description");

int desc_count = 0;
    for (int z = 0; z < model->columnCount(); z++)
    {
    QModelIndex index = model->index(0, z);
        if ( index.data(Qt::DisplayRole).toString() == desc )
        {
        desc_count += 1;
        int desc_ind = 1000 + desc_count;
        m[desc_ind] = z + 1;
        }
        else if ( index.data(Qt::DisplayRole).toString() == ignore)
        {
        m[999] = z + 1;
        }
        else
        {
            for ( int i = 1; i < headers.size(); ++i )
            {
                if ( index.data(Qt::DisplayRole).toString() == headers.value(i) )
                {
                m[i] = z + 1;
                }
            }
        }
    }
return m;
}

